package com.noahplat.file.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.file.utils.FastDFSUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName: FileController
 * @Description: 文件微服务对外接口
 * @Author 李景星
 * @Date 2021/3/6
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/file")
public class FileController {

    /**
     * 文件上传
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("hasAuthority('noahplat-service-file:FileController:upload')")
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam(value = "file") MultipartFile file) throws Exception {
        //文件字节数组
        byte[] file_buff = file.getBytes();
        //文件名字
        String filename = file.getOriginalFilename();
        //文件扩展名
        String file_etx_name = FilenameUtils.getExtension(filename);
        String[] uploadResult = FastDFSUtils.uploadFile(file_buff, file_etx_name, "诺亚健康平台");
        String url = FastDFSUtils.getServerUrl() + "/" + uploadResult[0] + "/" + uploadResult[1];
        return new Result(true, StatusCode.OK, "文件上传成功", url);
    }
}
